﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using ProjectWizardManagementv1.ViewModels.Concrete.Message;
using ProjectWizardManagementv1.WpfComponents.UserControls;
using CalendarEvent = ProjectWizardManagement.Entity.Model.CalendarEvent;

namespace ProjectWizardManagementv1.ViewModels.Concrete
{
    public class CalendarControlViewModel : ViewModelBase
    {
        private List<CalendarEvent> _calendarEvents;
        private CalendarEvent _calendarEventForm;

        public ObservableCollection<CalendarEventViewModel> CalendarEventViewModels { get; set; }

        public CalendarEvent CalendarEventForm
        {
            get { return _calendarEventForm; }
            set { _calendarEventForm = value; RaisePropertyChanged("CalendarEventForm"); }
        }

        public RelayCommand RemoveSelectedCalendarEvent { get; set; }
        public RelayCommand CreateNewCalendarEvent { get; set; }
        public RelayCommand ClearForm { get; set; }

        public CalendarControlViewModel()
        {
            CalendarEventViewModels = new ObservableCollection<CalendarEventViewModel>();
            CalendarEventForm = new CalendarEvent();
            CreateCommands();
            Messenger.Default.Register<List<CalendarEvent>>(this, SetCalendarEventModelList);
        }

        private void CreateCommands()
        {
            RemoveSelectedCalendarEvent = new RelayCommand(RemoveCalendarEventExecution, CalendarEventViewModels.Where(t=> t.IsSelected).Any);
            CreateNewCalendarEvent = new RelayCommand(AddCalendarEventExecution);
            ClearForm = new RelayCommand(ClearFormCalendarEventExecution);
        }

        private void SetCalendarEventModelList(List<CalendarEvent> tempEvents)
        {
            _calendarEvents = tempEvents;
            foreach (var calendarEvent in _calendarEvents)
            {
                var calendarModel = new CalendarEventViewModel(calendarEvent);
                CalendarEventViewModels.Add(calendarModel);
                Messenger.Default.Send(calendarModel);
                if(calendarEvent.ScheduledDate.CompareTo(DateTime.Now) >= 1 && calendarEvent.IsSendingNotification)
                    SendNotification(calendarModel);
            }
        }

        private void AddCalendarEventExecution()
        {
            _calendarEvents.Add(CalendarEventForm);
            var model = new CalendarEventViewModel(CalendarEventForm);
            CalendarEventViewModels.Add(model);
            Messenger.Default.Send(model);
            RaisePropertyChanged("CalendarEventViewModels");
            ClearFormCalendarEventExecution();
        }

        private void RemoveCalendarEventExecution()
        {
            var removeCalendarEventModel = CalendarEventViewModels.FirstOrDefault(t => t.IsSelected);
            Messenger.Default.Send(new RemoveCalendarDayMessage(removeCalendarEventModel));
            CalendarEventViewModels.Remove(removeCalendarEventModel);
            if (removeCalendarEventModel != null) _calendarEvents.Remove(removeCalendarEventModel.CalendarEvent);
            RaisePropertyChanged("CalendarEventViewModels");
        }

        private void ClearFormCalendarEventExecution()
        {
            CalendarEventForm = new CalendarEvent();
        }

        private void SendNotification(CalendarEventViewModel calendarEvent)
        {
            Messenger.Default.Send(new NotificationBoxMessage
                { Header = "Upcoming Event: " + calendarEvent.Name, Message = calendarEvent.Description});
        }

        private bool IsFormCalendarEventFilled()
        {
            return !string.IsNullOrWhiteSpace(CalendarEventForm.EventName) &&
                   !string.IsNullOrWhiteSpace(CalendarEventForm.Description);
        }
    }
}
